<template>
	<div class="system-edit-role-container">
		<el-dialog :title="(pd === 0 ? '添加' : '修改') + '会员等级'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="等级名称" prop="vipName">
							<el-input v-model="formData.vipName" placeholder="请输入等级名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="等级(1-10)" prop="level">
							<el-tree-select v-model="formData.level" :data="data" filterable />
							<!-- <el-input v-model="formData.level" placeholder="请输入等级" clearable></el-input> -->
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="充值累计" prop="recharge">
							<el-input v-model="formData.recharge" placeholder="请输入充值累计" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="流水累计" prop="flow">
							<el-input v-model="formData.flow" placeholder="请输入流水累计" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="比例1" prop="r1">
							<el-input v-model="formData.r1" placeholder="请输入比例1" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="比例2" prop="r2">
							<el-input v-model="formData.r2" placeholder="请输入比例2" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="比例3
"
							prop="r3"
						>
							<el-input
								v-model="formData.r3"
								placeholder="请输入比例3
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="比例4
"
							prop="r4"
						>
							<el-input
								v-model="formData.r4"
								placeholder="请输入比例4
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="比例5
"
							prop="r5"
						>
							<el-input
								v-model="formData.r5"
								placeholder="请输入比例5
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="比例6
"
							prop="r6"
						>
							<el-input
								v-model="formData.r6"
								placeholder="请输入比例6
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="比例7
"
							prop="r7"
						>
							<el-input
								v-model="formData.r7"
								placeholder="请输入比例7
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="图片地址
"
							prop="vipPic"
						>
						<MAa_add ref="editRoleRef" @customEvent="handleCustomEvent" v-model:imageUrl="formData.vipPic" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-switch
								v-model="formData.status"
								:active-value="1"
								:inactive-value="0"
								inline-prompt
								active-text="启"
								inactive-text="禁"
							></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" :loading="loading">{{ pd === 0 ? '新 增' : '修 改' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
	
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, getCurrentInstance, unref} from 'vue';
import { getRoleParams } from '/@/api/system/role';
import { ElMessage } from 'element-plus';
import { apiaddlevelList, apieditlevelList} from '/@/api/MemberManagement/MemberListlevel';
import MAa_add from '/@/views/Member/level/component/Pictures.vue';
interface DialogRow { 
	level:number;
	pdcs: number;
	id: number;
	vipName: string;
	status: number;
	recharge: number;
	flow: number;
	r1: number;
	r2: number;
	r3: number;
	r4: number;
	r5: number;
	r6: number;
	r7: number;
	vipPic: string;
}
interface arr {
	sourceData: Array<sourceDataff>;
}
interface sourceDataff {
	value: string;
	label: string;
}
interface RoleState {
	loading: boolean;
	isShowDialog: boolean;
	formData: DialogRow;
	menuData: arr;
	menuExpand: boolean;
	menuNodeAll: boolean;
	menuCheckStrictly: boolean;
	menuProps: {
		children: string;
		label: string;
	};
	rules: object;
}
export default defineComponent({
	name: 'systemEditRole',
	components: {MAa_add },
	props: {
		// 定义message为字符串类型
		pd: Number as () => number,
	},
	setup(props, { emit }) {
		const { proxy } = getCurrentInstance() as any;
		const formRef = ref<HTMLElement | null>(null);
		const menuRef = ref();
		// const emits = defineEmits(["getRoleList"]); 
		const state = reactive<RoleState>({
			loading: false,
			isShowDialog: false,
			menuData: {
				sourceData: [
					{ value: '1', label: '一级' },
					{ value: '2', label: '二级' },
					{ value: '3', label: '三级' },
					{ value: '4', label: '四级' },
					{ value: '5', label: '五级' },
					{ value: '6', label: '六级' },
					{ value: '7', label: '七级' },
					{ value: '8', label: '八级' },
					{ value: '9', label: '九级' },
					{ value: '10', label: '十级' },
				],
			},
			formData: {
				level:0,
				pdcs: NaN,
				id: 0,
				vipName: '',
				status: 1,
				recharge: 0,
				flow: 0,
				r1: 0,
				r2: 0,
				r3: 0,
				r4: 0,
				r5: 0,
				r6: 0,
				r7: 0,
				vipPic: '',
			},
			// 表单校验
			rules: {
				vipName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
			},

			menuExpand: false,
			menuNodeAll: false,
			menuCheckStrictly: false,
			menuProps: {
				children: 'children',
				label: 'title',
			},
		});
		const data = ref(state.menuData.sourceData);
		const handleCustomEvent = (data:any)=> {
			state.formData.vipPic = data.fileInfo.fileUrl; // 更新接收到的数据
    }
		// 打开弹窗
		const openDialog = (row?: DialogRow) => {
			resetForm();
			getMenuData();
			if (row) {
				state.formData = row;
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					state.loading = true;
					// state.formData.menuIds = getMenuAllCheckedKeys();
					if (props.pd === 0) {
						//添加
			
						apiaddlevelList(state.formData)
							.then(() => {
								ElMessage.success('添加等级成功');
								closeDialog(); // 关闭弹窗
							
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					} else {
						//修改
						apieditlevelList(state.formData,state.formData.id)
							.then(() => {
								ElMessage.success('修改等级成功');
								closeDialog(); // 关闭弹窗
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					}
				}
			});
		};
		// 获取菜单结构数据
		const getMenuData = () => {
			getRoleParams().then((res: any) => {
				state.menuData = proxy.handleTree(res.data.menu, 'id', 'pid');
			});
		};
		const resetForm = () => {
			state.menuCheckStrictly = false;
			state.menuExpand = false;
			state.menuNodeAll = false;
			state.formData = {
				pdcs: NaN,
				id: 0,
				vipName: '',
				status: 1,
				recharge: 0,
				flow: 0,
				r1: 0,
				r2: 0,
				r3: 0,
				r4: 0,
				r5: 0,
				r6: 0,
				r7: 0,
				vipPic: '',
				level: 0
			};
		};

		return {
			data,
			openDialog,
			handleCustomEvent,
			closeDialog,
			onCancel,
			onSubmit,
			menuRef,
			formRef,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.tree-border {
	margin-top: 5px;
	border: 1px solid #e5e6e7 !important;
	border-radius: 4px;
}
.system-edit-role-container {
	.menu-data-tree {
		border: var(--el-input-border, var(--el-border-base));
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
